package com.facebook.imagepipeline.producers;

import android.graphics.Bitmap;
import com.facebook.common.internal.ImmutableMap;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.references.CloseableReference;
import com.facebook.common.util.UriUtil;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.decoder.ImageDecoder;
import com.facebook.imagepipeline.decoder.ProgressiveJpegConfig;
import com.facebook.imagepipeline.decoder.ProgressiveJpegParser;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.CloseableStaticBitmap;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.image.ImmutableQualityInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.facebook.imagepipeline.memory.ByteArrayPool;
import com.facebook.imagepipeline.request.ImageRequest;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

public class DecodeProducer
  implements Producer<CloseableReference<CloseableImage>>
{
  private static final String BITMAP_SIZE_KEY = "bitmapSize";
  private static final String HAS_GOOD_QUALITY_KEY = "hasGoodQuality";
  private static final String IMAGE_TYPE_KEY = "imageType";
  private static final String IS_FINAL_KEY = "isFinal";
  public static final String PRODUCER_NAME = "DecodeProducer";
  private final ByteArrayPool mByteArrayPool;
  private final boolean mDownsampleEnabled;
  private final boolean mDownsampleEnabledForNetwork;
  private final Executor mExecutor;
  private final ImageDecoder mImageDecoder;
  private final Producer<EncodedImage> mInputProducer;
  private final ProgressiveJpegConfig mProgressiveJpegConfig;

  public DecodeProducer(ByteArrayPool paramByteArrayPool, Executor paramExecutor, ImageDecoder paramImageDecoder, ProgressiveJpegConfig paramProgressiveJpegConfig, boolean paramBoolean1, boolean paramBoolean2, Producer<EncodedImage> paramProducer)
  {
    this.mByteArrayPool = ((ByteArrayPool)Preconditions.checkNotNull(paramByteArrayPool));
    this.mExecutor = ((Executor)Preconditions.checkNotNull(paramExecutor));
    this.mImageDecoder = ((ImageDecoder)Preconditions.checkNotNull(paramImageDecoder));
    this.mProgressiveJpegConfig = ((ProgressiveJpegConfig)Preconditions.checkNotNull(paramProgressiveJpegConfig));
    this.mDownsampleEnabled = paramBoolean1;
    this.mDownsampleEnabledForNetwork = paramBoolean2;
    this.mInputProducer = ((Producer)Preconditions.checkNotNull(paramProducer));
  }

  public void produceResults(Consumer<CloseableReference<CloseableImage>> paramConsumer, ProducerContext paramProducerContext)
  {
    if (!UriUtil.isNetworkUri(paramProducerContext.getImageRequest().getSourceUri()));
    for (Object localObject = new LocalImagesProgressiveDecoder(paramConsumer, paramProducerContext); ; localObject = new NetworkImagesProgressiveDecoder(paramConsumer, paramProducerContext, new ProgressiveJpegParser(this.mByteArrayPool), this.mProgressiveJpegConfig))
    {
      this.mInputProducer.produceResults((Consumer)localObject, paramProducerContext);
      return;
    }
  }

  private class LocalImagesProgressiveDecoder extends DecodeProducer.ProgressiveDecoder
  {
    public LocalImagesProgressiveDecoder(ProducerContext arg2)
    {
      super(localConsumer, localProducerContext);
    }

    protected int getIntermediateImageEndOffset(EncodedImage paramEncodedImage)
    {
      return paramEncodedImage.getSize();
    }

    protected QualityInfo getQualityInfo()
    {
      return ImmutableQualityInfo.of(0, false, false);
    }

    protected boolean updateDecodeJob(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      boolean bool2;
      if (!paramBoolean)
        bool2 = false;
      while (true)
      {
        return bool2;
        try
        {
          boolean bool1 = super.updateDecodeJob(paramEncodedImage, paramBoolean);
          bool2 = bool1;
        }
        finally
        {
        }
      }
    }
  }

  private class NetworkImagesProgressiveDecoder extends DecodeProducer.ProgressiveDecoder
  {
    private int mLastScheduledScanNumber;
    private final ProgressiveJpegConfig mProgressiveJpegConfig;
    private final ProgressiveJpegParser mProgressiveJpegParser;

    public NetworkImagesProgressiveDecoder(ProducerContext paramProgressiveJpegParser, ProgressiveJpegParser paramProgressiveJpegConfig, ProgressiveJpegConfig arg4)
    {
      super(paramProgressiveJpegParser, paramProgressiveJpegConfig);
      Object localObject1;
      this.mProgressiveJpegParser = ((ProgressiveJpegParser)Preconditions.checkNotNull(localObject1));
      Object localObject2;
      this.mProgressiveJpegConfig = ((ProgressiveJpegConfig)Preconditions.checkNotNull(localObject2));
      this.mLastScheduledScanNumber = 0;
    }

    protected int getIntermediateImageEndOffset(EncodedImage paramEncodedImage)
    {
      return this.mProgressiveJpegParser.getBestScanEndOffset();
    }

    protected QualityInfo getQualityInfo()
    {
      return this.mProgressiveJpegConfig.getQualityInfo(this.mProgressiveJpegParser.getBestScanNumber());
    }

    protected boolean updateDecodeJob(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      while (true)
      {
        try
        {
          bool1 = super.updateDecodeJob(paramEncodedImage, paramBoolean);
          if ((!paramBoolean) && (EncodedImage.isValid(paramEncodedImage)))
          {
            boolean bool2 = this.mProgressiveJpegParser.parseMoreData(paramEncodedImage);
            if (!bool2)
              bool1 = false;
          }
          else
          {
            return bool1;
          }
          int i = this.mProgressiveJpegParser.getBestScanNumber();
          if ((i > this.mLastScheduledScanNumber) && (i >= this.mProgressiveJpegConfig.getNextScanNumberToDecode(this.mLastScheduledScanNumber)))
          {
            this.mLastScheduledScanNumber = i;
            continue;
          }
        }
        finally
        {
        }
        boolean bool1 = false;
      }
    }
  }

  private abstract class ProgressiveDecoder extends DelegatingConsumer<EncodedImage, CloseableReference<CloseableImage>>
  {
    private final ImageDecodeOptions mImageDecodeOptions;

    @GuardedBy("this")
    private boolean mIsFinished;
    private final JobScheduler mJobScheduler;
    private final ProducerContext mProducerContext;
    private final ProducerListener mProducerListener;

    public ProgressiveDecoder(ProducerContext arg2)
    {
      super();
      final ProducerContext localProducerContext;
      this.mProducerContext = localProducerContext;
      this.mProducerListener = localProducerContext.getListener();
      this.mImageDecodeOptions = localProducerContext.getImageRequest().getImageDecodeOptions();
      this.mIsFinished = false;
      JobScheduler.JobRunnable local1 = new JobScheduler.JobRunnable()
      {
        public void run(EncodedImage paramAnonymousEncodedImage, boolean paramAnonymousBoolean)
        {
          if (paramAnonymousEncodedImage != null)
          {
            if (DecodeProducer.this.mDownsampleEnabled)
            {
              ImageRequest localImageRequest = localProducerContext.getImageRequest();
              if ((DecodeProducer.this.mDownsampleEnabledForNetwork) || (!UriUtil.isNetworkUri(localImageRequest.getSourceUri())))
                paramAnonymousEncodedImage.setSampleSize(DownsampleUtil.determineSampleSize(localImageRequest, paramAnonymousEncodedImage));
            }
            DecodeProducer.ProgressiveDecoder.this.doDecode(paramAnonymousEncodedImage, paramAnonymousBoolean);
          }
        }
      };
      this.mJobScheduler = new JobScheduler(DecodeProducer.this.mExecutor, local1, this.mImageDecodeOptions.minDecodeIntervalMs);
      this.mProducerContext.addCallbacks(new BaseProducerContextCallbacks()
      {
        public void onIsIntermediateResultExpectedChanged()
        {
          if (DecodeProducer.ProgressiveDecoder.this.mProducerContext.isIntermediateResultExpected())
            DecodeProducer.ProgressiveDecoder.this.mJobScheduler.scheduleJob();
        }
      });
    }

    private void doDecode(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      if ((isFinished()) || (!EncodedImage.isValid(paramEncodedImage)))
        return;
      try
      {
        long l = this.mJobScheduler.getQueuedTime();
        int i;
        if (paramBoolean)
          i = paramEncodedImage.getSize();
        while (true)
        {
          QualityInfo localQualityInfo;
          if (paramBoolean)
          {
            localQualityInfo = ImmutableQualityInfo.FULL_QUALITY;
            this.mProducerListener.onProducerStart(this.mProducerContext.getId(), "DecodeProducer");
          }
          try
          {
            CloseableImage localCloseableImage = DecodeProducer.this.mImageDecoder.decodeImage(paramEncodedImage, i, localQualityInfo, this.mImageDecodeOptions);
            Map localMap2 = getExtraMap(localCloseableImage, l, localQualityInfo, paramBoolean);
            this.mProducerListener.onProducerFinishWithSuccess(this.mProducerContext.getId(), "DecodeProducer", localMap2);
            handleResult(localCloseableImage, paramBoolean);
            return;
            i = getIntermediateImageEndOffset(paramEncodedImage);
            continue;
            localQualityInfo = getQualityInfo();
          }
          catch (Exception localException)
          {
            Map localMap1 = getExtraMap(null, l, localQualityInfo, paramBoolean);
            this.mProducerListener.onProducerFinishWithFailure(this.mProducerContext.getId(), "DecodeProducer", localException, localMap1);
            handleError(localException);
            return;
          }
        }
      }
      finally
      {
        EncodedImage.closeSafely(paramEncodedImage);
      }
    }

    private Map<String, String> getExtraMap(@Nullable CloseableImage paramCloseableImage, long paramLong, QualityInfo paramQualityInfo, boolean paramBoolean)
    {
      if (!this.mProducerListener.requiresExtraMap(this.mProducerContext.getId()))
        return null;
      String str1 = String.valueOf(paramLong);
      String str2 = String.valueOf(paramQualityInfo.isOfGoodEnoughQuality());
      String str3 = String.valueOf(paramBoolean);
      String str4 = String.valueOf(this.mProducerContext.getImageRequest().getCacheChoice());
      if ((paramCloseableImage instanceof CloseableStaticBitmap))
      {
        Bitmap localBitmap = ((CloseableStaticBitmap)paramCloseableImage).getUnderlyingBitmap();
        return ImmutableMap.of("bitmapSize", localBitmap.getWidth() + "x" + localBitmap.getHeight(), "queueTime", str1, "hasGoodQuality", str2, "isFinal", str3, "imageType", str4);
      }
      return ImmutableMap.of("queueTime", str1, "hasGoodQuality", str2, "isFinal", str3, "imageType", str4);
    }

    private void handleCancellation()
    {
      maybeFinish(true);
      getConsumer().onCancellation();
    }

    private void handleError(Throwable paramThrowable)
    {
      maybeFinish(true);
      getConsumer().onFailure(paramThrowable);
    }

    private void handleResult(CloseableImage paramCloseableImage, boolean paramBoolean)
    {
      CloseableReference localCloseableReference = CloseableReference.of(paramCloseableImage);
      try
      {
        maybeFinish(paramBoolean);
        getConsumer().onNewResult(localCloseableReference, paramBoolean);
        return;
      }
      finally
      {
        CloseableReference.closeSafely(localCloseableReference);
      }
    }

    private boolean isFinished()
    {
      try
      {
        boolean bool = this.mIsFinished;
        return bool;
      }
      finally
      {
        localObject = finally;
        throw localObject;
      }
    }

    private void maybeFinish(boolean paramBoolean)
    {
      if (paramBoolean);
      try
      {
        if (this.mIsFinished)
          return;
        getConsumer().onProgressUpdate(1.0F);
        this.mIsFinished = true;
        this.mJobScheduler.clearJob();
        return;
      }
      finally
      {
      }
    }

    protected abstract int getIntermediateImageEndOffset(EncodedImage paramEncodedImage);

    protected abstract QualityInfo getQualityInfo();

    public void onCancellationImpl()
    {
      handleCancellation();
    }

    public void onFailureImpl(Throwable paramThrowable)
    {
      handleError(paramThrowable);
    }

    public void onNewResultImpl(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      if ((paramBoolean) && (!EncodedImage.isValid(paramEncodedImage)))
        handleError(new NullPointerException("Encoded image is not valid."));
      while ((!updateDecodeJob(paramEncodedImage, paramBoolean)) || ((!paramBoolean) && (!this.mProducerContext.isIntermediateResultExpected())))
        return;
      this.mJobScheduler.scheduleJob();
    }

    protected void onProgressUpdateImpl(float paramFloat)
    {
      super.onProgressUpdateImpl(0.99F * paramFloat);
    }

    protected boolean updateDecodeJob(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      return this.mJobScheduler.updateJob(paramEncodedImage, paramBoolean);
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.producers.DecodeProducer
 * JD-Core Version:    0.6.2
 */